package com.kinyx.framework.db.sentence.res;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.kinyx.framework.annotation.assist.Tester;
import com.kinyx.framework.db.configs.DBConfigs;
import com.kinyx.framework.utils.KArrayUtils;
import com.kinyx.framework.utils.KListUtils;

/**
 * 排序子句
 */
@Tester("com.kinyx.framework.db.sentence.res._ORDER_BY_Tester_")
class _ORDER_BY extends _Sentence {

	/** 排序信息对象列表 */
	private List<Order> orders = new ArrayList<>();

	_ORDER_BY(final Order order, final Order... orders) {
		this.orders.add(order);
		if (!KArrayUtils.isEmpty(orders)) {
			this.orders.addAll(Arrays.asList(orders));
		}
	}

	_ORDER_BY addOrders(final Order order, final Order... orders) {
		this.orders.add(order);
		if (!KArrayUtils.isEmpty(orders)) {
			this.orders.addAll(Arrays.asList(orders));
		}
		return this;
	}

	@Override
	public String getSentence(final DBConfigs configs, final boolean isSQL, final boolean withTableAlias) {
		return "ORDER BY" + KListUtils.join(this.orders, ",", (order, index) -> { return " " + order.getSentence(configs, isSQL, withTableAlias); });
	}

}
